﻿using Force.DeepCloner;
using gptDataType.Helper;
using gptDataType.Interface;
using gptDataType.Models;
using gptDataType.Type.bit;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Reflection.PortableExecutable;
using System.Text;
using System.Threading.Tasks;

namespace gptDataType.Type.combination
{
    public class PlcArray : PlcComplexBase//PlcMemberBase
    {
        /// <summary>
        /// 数组元素
        /// </summary>
        //private  List<IPlcMember> _elements;
       // private readonly List<IPlcMember> _elements = new List<IPlcMember>();

        /// <summary>
        /// 数组占用字节数
        /// </summary>
       // private readonly int _elementSizeInBytes;

        /// <summary>
        /// 数组元素类型
        /// </summary>
       // public DataType ElementType { get; }

        /// <summary>
        /// 数组的长度
        /// </summary>
        /// 
        public IPlcMember prototype { get; set; }
        public DataType MemberDataType { get; private set; }
        public int Length => GetMembers().Count;

        //public IReadOnlyList<IPlcMember> Elements => _elements;

        public PlcArray(string name, IPlcMember element, int length)
            : base(name, DataType.Array, CalculateTotalSize(element, length))
        {
            // 验证原型有效性
            if (element == null) throw new ArgumentNullException(nameof(element));
            if (length <= 0) throw new ArgumentOutOfRangeException(nameof(length));

            // 初始化元素
            MemberDataType = element.DataType;
            int _elementSizeInBytes = element.SizeInBytes();
            //_members = new List<IPlcMember>();
 

            // 创建元素实例并设置地址
            for (int i = 0; i < length; i++)
            {
                var _element = element.DeepClone();
                _element.Name = $"{name}[{i}]";
                _element.Address = new PlcAddress(
                    byteOffset: i * _elementSizeInBytes,
                    bitOffset: 0);
                _element.Parent = this;
                //_elements[i] = element;
                this.AddMember(_element);
            }
            prototype = element.DeepClone();
            prototype.Name = this.Name;
            prototype.Address = this.Address;
            prototype.Symbol = this.Symbol;
        }

 

        private static int CalculateTotalSize(IPlcMember prototype, int length)
        {
            return prototype.SizeInBits * length;
        }

        //public void compileAddr(int byteOffset = 0)
        //{

        //    // int _bitOffset = 0;
        //    int _byteOffset = byteOffset;
        //    for (int i = 0; i < _elements.Count; i++)
        //    {
        //        //结构体类型的处理
        //        if (_elements[i] is PlcStruct pst)
        //        {
        //            pst.Address = new PlcAddress(_byteOffset);
        //            pst.compileAddr(_byteOffset);
        //            var test = _elements[i].Name;
        //            var test2 = _byteOffset;
        //            _byteOffset += pst.SizeInBytes();
        //        }
        //        //数组类型的处理
        //        else if (_elements[i] is PlcArray pa)
        //        {
        //            pa.Address = new PlcAddress(_byteOffset);
        //            pa.compileAddr(_byteOffset);
        //            _byteOffset += pa.SizeInBytes();
        //        }
        //        else
        //        {
        //            var test = _elements[i];
        //            _elements[i].Address = new PlcAddress(_byteOffset);
        //            _byteOffset += _elements[i].SizeInBytes();
        //        }
        //    }
        //}


    }
}
